import { Element } from "./ElementDefine";
import { BaseFileGenerate } from "./FileGenerate";
import * as vscode from 'vscode';

const serviceImplInterface = `
_package
_imports

_comment
@Service
public class _serviceImplName extends ServiceImpl<_mapperName, _className> implements _serviceName {

}
`;


const mapperComment: string = `
/**
 * _tableComment serviceImpl
 *
 * @author _author
 */`;


/**
 * 生成serviceImpl接口文件
 */
export class ServiceImplFileGenerate extends BaseFileGenerate {

    init(): void {
        this.elements.set("package", new Element("package _packagePath;\n", ["_packagePath"],
            options => {
                let path: string[] = [];
                if (options.parentPackage) {
                    path.push(options.parentPackage);
                }
                if (options.servicePath) {
                    path.push(options.servicePath);
                }
                path.push("impl");
                return [path.join(".")];
            }));

        this.elements.set("classComment", new Element(mapperComment, ['_tableComment', '_author'],
            (options, columnInfo) => [columnInfo.tableComment ? columnInfo.tableComment : columnInfo.tableName, options.author],
            () => true));

        this.dynamicElements.imports.push("import org.springframework.stereotype.Service;\n");
        this.dynamicElements.imports.push("import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;\n");
    }

    weaveContent(): void {
        let packageContent = this.elements.get("package")?.handleContent(this.options, this.columnInfos[0], this.columnInfos) || '';
        let classCommentContent = this.elements.get("classComment")?.handleContent(this.options, this.columnInfos[0], this.columnInfos) || '';

        let className = this.columnInfos[0].className;
        let mapperName = this.columnInfos[0].mapperName;
        let serviceName = this.columnInfos[0].serviceName;
        let serviceImplName = this.columnInfos[0].serviceImplName;

        this.dynamicElements.imports.push(this.getImport(this.options.parentPackage, this.options.entityPath, this.columnInfos[0].className));
        this.dynamicElements.imports.push(this.getImport(this.options.parentPackage, this.options.interfacePath, this.columnInfos[0].mapperName));
        this.dynamicElements.imports.push(this.getImport(this.options.parentPackage, this.options.servicePath, this.columnInfos[0].serviceName));

        let importsContent = Array.from(new Set(this.dynamicElements.imports)).sort((a, b) => a.localeCompare(b)).join("");

        let elementFlag = ["_package", "_imports", "_comment", "_serviceImplName", "_className", "_mapperName", "_serviceName"];
        let classElement = new Element(serviceImplInterface, elementFlag,
            () => [packageContent, importsContent, classCommentContent, serviceImplName, className, mapperName, serviceName]);
        let serviceImplContent = classElement.handleContent(this.options, this.columnInfos[0], this.columnInfos);
        console.log("生成serviceImpl " + serviceImplName);
        this.content = serviceImplContent;
    }

    getDirectory(projectPath: vscode.Uri): vscode.Uri {
        return vscode.Uri.joinPath(projectPath, this.mainPath,
            this.options.parentPackage.replace(/\./g, '/'),
            this.options.servicePath.replace(/\./g, '/') + '/impl'
        );
    }

    getFileName(): string {
        return this.columnInfos[0].serviceImplName + '.java';
    }

}